<?php
namespace app\common\util\SmsCode\impl;
use app\common\util\SmsCode\SmsCodeMapper;
use think\facade\Cache;

class TestSmsCodeCodeImpl implements SmsCodeMapper
{
    const sms_prefix ="_sms_" ;
    const ttl = 3000;
    static function buildKey($type,$smsSn){
        return self::sms_prefix.$type."_".$smsSn;
    }

    static function send($mobile,$type): array
    {
        // TODO: Implement sender() method.
        $smsCode = rand(100000,999999);
        $smsSn=md5($smsCode).time();
        cache(self::buildKey($type,$smsSn),$smsCode,["expire"=>self::ttl]);
        return [
            "smsCode"=>$smsCode,
            "smsSn"=>$smsSn,
            "ttl"=>self::ttl
        ];

    }

    public static function checkIsOk($smsSn, $smsCode,$type): bool
    {
        //dd($smsCode);
        // TODO: Implement checkIsOk() method.
        $rsIsRight= cache(self::buildKey($type,$smsSn))==$smsCode;
        if ($rsIsRight)Cache::delete(self::buildKey($type,$smsSn));
        return $rsIsRight;
    }
}